<style>
    .label-name{font-size: 13px;color: #333; font-weight: normal;}
    .username-box{font-size: 12px;line-height: 12px;padding: 4px 10px 4px 10px; margin: 0px 5px 5px 0px; border-radius: 16px; display: inline-block; border: 1px solid #e5e5e5;}
    .delete{display: none;}
    .username-box:hover .delete{display: inline-block; font-size: 16px;}
</style>
<div class="forum-page">
    <style></style>
    <div class="add-manage" style="border-top: 2px solid #57a957;">
        <div class="weui-cells__title">增加版主</div>
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
                <div class="weui-cell__hd">
                    <label class="weui-label" style="font-size: 13px; color: #333; font-weight: normal">增加版主</label>
                </div>
                <div class="weui-cell__hd" style="width: 20%">
                    <input id="userid" type="text" class="weui-input" placeholder="请输入手机号码" style="border: 1px solid #999">
                </div>
                <div class="weui-cell__hd" style="width: 100px; margin-left: 15px;">
                    <select id="label-list">
                    </select>
                </div>
                <div class="weui-cell__hd">
                    <a id="invite" href="javascript:void (0)" style="font-size: 13px; color: white;" class="weui-btn weui-btn_mini weui-btn_primary">邀请</a>
                </div>
            </div>
        </div>
        <div class="weui-cells__title">增加板块</div>
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
                <div class="weui-cell__hd">
                    <label class="weui-label" style="font-size: 13px; color: #333; font-weight: normal">板块名</label>
                </div>
                <div class="weui-cell__hd" style="width: 20%">
                    <input id="label-name" type="text" class="weui-input" placeholder="板块名称" style="border: 1px solid #999;">
                </div>
                <div class="weui-cell__hd" style="min-width: 100px; margin-left: 15px;"></div>
                <div class="weui-cell__hd">
                    <a id="add-label" href="javascript:void (0)" style="font-size: 13px; color: white" class="weui-btn weui-btn_mini weui-btn_primary">增加</a>
                </div>
            </div>
        </div>
    </div>
    <div class="banzu-managed" style="border-top: 2px solid #57a957; margin-top: 15px;">
        <div class="weui-cells__title">版主管理</div>
        <div class="">
            <div class="weui-cell weui-cell_access">
                <div class="weui-cell__bd">
                    <small>论坛名称</small>
                </div>
                <div class="weui-cell__bd">
                    <small>用户名</small>
                </div>
            </div>
            <div class="bc-list" id="moderator" data-port="http://api.hongyanche.com/forum_admin/moderator_list">
                <!--<div class="weui-cell__bd label-item forum-label">人*车*故事</div>-->
                <!--<div class="weui-cell__bd label-item label-item-user">某一个用户</div>-->
                <div class="bc-list-template">
                    <div class="weui-cell weui-cell_access">
                        <div class="weui-cell__bd label-name"><cn list=1></cn></div>
                        <div class="weui-cell__bd label-name">
                            <moderator_name list=r>
                                <span class="username-box">
                                    <user_name range=1></user_name>
                                    <user_id range=1 class="hidden"></user_id>
                                    <i class="weui-icon weui-icon-cancel weui-icon-original js-delete delete" style="font-size: 14px   "></i>
                                    <label_id range=1 class="hidden"></label_id>
                                </span>
                            </moderator_name>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="forum" style="border-top: 2px solid #57a957; margin-top: 15px;">
        <div class="weui-cells__title">板块管理</div>
        <div class="weui-table" id="forum-name">
            <!--<div class="weui-cell">-->
                <!--<div class="weui-cell__bd">-->
                    <!--<small>板块名</small>-->
                <!--</div>-->
                <!--<div class="weui-cell__bd">-->
                    <!--<a href="javascript:void (0)" style="font-size: 13px; color: white; vertical-align: middle;" class="weui-btn weui-btn_mini weui-btn_primary">修改名称</a>-->
                    <!--<label class="weui-cell__ft">-->
                        <!--<input type="checkbox" class="weui-switch" style="vertical-align: middle;">-->
                    <!--</label>-->
                <!--</div>-->
            <!--</div>-->
        </div>
    </div>
</div>

<sss>
    ;(function (w, u) {
        // 论坛
        function initPage(e) {
            if (e == 'init') {
                $.ajax({
                    url: 'http://api.hongyanche.com/forum_admin/label_list',
                    type: 'GET',
    xhrFields:{
    withCredentials:true
    },
                    success: function (data) {
                        var datas = data.data.list;
                        var forumHTML = null;
                        datas.forEach(function (item, index) {
                            $('#label-list').append("<option value="+item.label_id+">"+item.cn+"</option>");
                            if (item.released == 0) {
                                $('#forum-name').append(`
                        <div class="weui-cell">
                            <div class="weui-cell__bd">
                                <small class="" data-id="${item.label_id}">${item.cn}</small>
                                <input class="input-el" type="text" style="display: none">
                            </div>
                            <div class="weui-cell__bd">
                                <a href="javascript:void (0)" style="font-size: 13px; color: white; vertical-align: middle;" class="js-forum-name weui-btn weui-btn_mini weui-btn_primary" data-id="${item.label_id}">修改名称</a>
                                <label class="weui-cell__ft">
                                    是否开放
                                    <input type="checkbox" class="weui-switch switch" style="vertical-align: middle;" data-id="${item.label_id}" >
                                </label>
                            </div>
                        </div>
                    `)
                            }
                            if (item.released == 1 && !item.admin) {
                                $('#forum-name').append(`
                        <div class="weui-cell">
                            <div class="weui-cell__bd">
                                <small class="" data-id="${item.label_id}">${item.cn}</small>
                                <input class="input-el" type="text" style="display: none">
                            </div>
                            <div class="weui-cell__bd">
                                <a href="javascript:void (0)" style="font-size: 13px; color: white; vertical-align: middle;" class="js-forum-name weui-btn weui-btn_mini weui-btn_primary" data-id="${item.label_id}">修改名称</a>
                                <label class="weui-cell__ft">
                                    是否开放
                                    <input type="checkbox" class="weui-switch switch" style="vertical-align: middle;" data-id="${item.label_id}" checked >
                                </label>
                                <label class="weui-cell__ft">
                                    只允许管理员发帖
                                    <input type="checkbox" class="weui-switch admin" style="vertical-align: middle;" data-id="${item.label_id}" >
                                </label>
                            </div>
                        </div>
                    `)
                            }
                            if (!item.released && item.admin == 1) {
                                $('#forum-name').append(``);
                            }
                            if (item.released && item.admin == 1) {
                                $('#forum-name').append(`
                        <div class="weui-cell">
                            <div class="weui-cell__bd">
                                <small class="" data-id="${item.label_id}">${item.cn}</small>
                                <input class="input-el" type="text" style="display: none">
                            </div>
                            <div class="weui-cell__bd">
                                <a href="javascript:void (0)" style="font-size: 13px; color: white; vertical-align: middle;" class="js-forum-name weui-btn weui-btn_mini weui-btn_primary" data-id="${item.label_id}">修改名称</a>
                                <label class="weui-cell__ft">
                                    是否开放
                                    <input type="checkbox" class="weui-switch switch" style="vertical-align: middle;" data-id="${item.label_id}" checked >
                                </label>
                                <label class="weui-cell__ft">
                                    只允许管理员发帖
                                    <input type="checkbox" class="weui-switch admin" style="vertical-align: middle;" data-id="${item.label_id}" checked>
                                </label>
                            </div>
                        </div>
                    `)
                            }
                        });

                    }
                });
            } else if (e == 'reload') {
                $.ajax({
                    url: 'http://api.hongyanche.com/forum_admin/label_list',
                    type: 'GET',
    xhrFields:{
    withCredentials:true
    },
                    success: function (data) {
                        var datas = data.data.list;
                        $('#label-list').empty();
                        $('#forum-name').empty();
                        datas.forEach(function (item, index) {
                            $('#label-list').append("<option value="+item.label_id+">"+item.cn+"</option>");
                            if (item.released == 0) {
                                $('#forum-name').append(`
                        <div class="weui-cell">
                            <div class="weui-cell__bd">
                                <small class="" data-id="${item.label_id}">${item.cn}</small>
                                <input type="text" style="display: none">
                            </div>
                            <div class="weui-cell__bd">
                                <a href="javascript:void (0)" style="font-size: 13px; color: white; vertical-align: middle;" class="js-forum-name weui-btn weui-btn_mini weui-btn_primary" data-id="${item.label_id}">修改名称</a>
                                <label class="weui-cell__ft">
                                    是否开放
                                    <input type="checkbox" class="weui-switch switch" style="vertical-align: middle;" data-id="${item.label_id}" >
                                </label>
                            </div>
                        </div>
                    `)
                            }
                            if (item.released && item.admin == 1) {
                                $('#forum-name').append(`
                        <div class="weui-cell">
                            <div class="weui-cell__bd">
                                <small class="" data-id="${item.label_id}">${item.cn}</small>
                                <input class="input-el" type="text" style="display: none">
                            </div>
                            <div class="weui-cell__bd">
                                <a href="javascript:void (0)" style="font-size: 13px; color: white; vertical-align: middle;" class="js-forum-name weui-btn weui-btn_mini weui-btn_primary" data-id="${item.label_id}">修改名称</a>
                                <label class="weui-cell__ft">
                                    是否开放
                                    <input type="checkbox" class="weui-switch switch" style="vertical-align: middle;" data-id="${item.label_id}" checked >
                                </label>
                                <label class="weui-cell__ft">
                                    只允许管理员发帖
                                    <input type="checkbox" class="weui-switch admin" style="vertical-align: middle;" data-id="${item.label_id}" checked>
                                </label>
                            </div>
                        </div>
                    `)
                            }
                            if (item.released == 1 && !item.admin) {
                                $('#forum-name').append(`
                        <div class="weui-cell">
                            <div class="weui-cell__bd">
                                <small class="" data-id="${item.label_id}">${item.cn}</small>
                                <input type="text" style="display: none">
                            </div>
                            <div class="weui-cell__bd">
                                <a href="javascript:void (0)" style="font-size: 13px; color: white; vertical-align: middle;" class="js-forum-name weui-btn weui-btn_mini weui-btn_primary" data-id="${item.label_id}">修改名称</a>
                                <label class="weui-cell__ft">
                                    是否开放
                                    <input type="checkbox" class="weui-switch switch" style="vertical-align: middle;" data-id="${item.label_id}" checked >
                                </label>
                                <label class="weui-cell__ft">
                                    只允许管理员发帖
                                    <input type="checkbox" class="weui-switch admin" style="vertical-align: middle;" data-id="${item.label_id}" >
                                </label>
                            </div>
                        </div>
                    `)
                            }
                            if ( !item.released && item.admin == 1) {
                                $('#forum-name').append(``);
                            }

                        });

                    }
                });
            }
        }
            initPage('init');
        // 增加版主
        $('.add-manage').on('click', '#invite', function () {
            var userID = $('#userid').val();
            if (!userID) return alert('请输入');
            var labelID = $('#label-list').val();
            $.ajax({
                url: 'http://api.hongyanche.com/forum_admin/invite_moderator',
                type: 'POST',
    xhrFields:{
    withCredentials:true
    },
                data: {
                    label_id: labelID,
                    tel: userID
                },
                success: function (data) {
                    alert(data.errormsg);
                },
                error: function (err) {
                    return console.log('出错了：' + err)
                }
            })
        });
        // 增加论坛板块
        $('.add-manage').on('click', '#add-label', function () {
            var labelName = $('#label-name').val();
            $.ajax({
                url: 'http://api.hongyanche.com/forum_admin/label_add',
                type: 'POST',
    xhrFields:{
    withCredentials:true
    },
                data: {
                    cn: labelName
                },
                success: function (data) {
                    alert(data.errormsg);
                    initPage('reload');
                },
                error: function (err) {
                    return console.log('出错了:' + err)
                }
            })
        });
        // moderator_remove
        // 撤销版主权限
        $('#moderator').on('click', '.js-delete', function () {
            var labelID = $(this).next().text();
            var userID = $(this).prev().text();
            var userName = $(this).prev().prev().text();
            var Delete = confirm('是否要撤销' + userName + '的版主权限?');
            if (Delete) {
                $.ajax({
                    url: 'http://api.hongyanche.com/forum_admin/moderator_remove',
                    type: 'POST',
    xhrFields:{
    withCredentials:true
    },
                    data: {
                        user_id: userID,
                        label_id: labelID
                    },
                    success: function () {
                        LIST.reload('#moderator');
                    }
                })
            }
        });

        // 板块管理
        $('#forum-name').on('click', '.js-forum-name', function () {
            var inputDom = $(this).parent().prev().find('input');
            var text = $(this).parent().prev().find('small');
            var labelID = $(this).attr('data-id');
            var a = $(this);
            var KEY = true;
            $('.input-el').hide();
                inputDom.show();
                $('.ok-btn').hide();
                $('.no-btn').hide();
                if (KEY) {
                    $('.js-forum-name').show();
                    $(this).hide();
                    $(this).after(`
                            <a href="javascript:void (0)" style="font-size: 13px; color: white; vertical-align: middle;" class="ok-btn btn weui-btn weui-btn_mini weui-btn_primary" >确认</a>
                            <a href="javascript:void (0)" style="font-size: 13px; color: white; vertical-align: middle;" class="no-btn btn weui-btn weui-btn_mini weui-btn_primary" >取消</a>
                    `);
                    KEY = false;
                    $('#forum-name').on('click','.ok-btn' ,function () {
                        var thisBTN = $(this);
                        $.ajax({
                            url: 'http://api.hongyanche.com/forum_admin/label_rename',
                            type: 'POST',
    xhrFields:{
    withCredentials:true
    },
                            data: {
                                label_id: labelID,
                                newname: inputDom.val()
                            },
                            success: function (data) {
                                $.ajax({
                                    url: 'http://api.hongyanche.com/forum_admin/label_list',
                                    type: 'GET',
    xhrFields:{
    withCredentials:true
    },
                                    success: function (data) {
                                        var datas = data.data.list;
                                        var optItem = null;
                                        text.text(inputDom.val());
                                        inputDom.hide();
                                        thisBTN.hide();
                                        a.show();
                                        $('.no-btn').hide();
                                        KEY = false;
                                        datas.forEach(function (item, index) {
                                            optItem += `<option value="${item.label_id}">${item.cn}</option>`;
                                            $('#label-list').html(optItem);
                                        });
                                    }
                                });
                            }
                        })
                    });
                    $('#forum-name').on('click','.no-btn' ,function () {
                        $('.btn').hide();
                        a.show();
                        inputDom.hide();
                    })
                }
        });
        // switch 开关
        $('#forum-name').on('change', '.switch', function () {
            var ID = $(this).attr('data-id');
            var bischecked=$($(this)).is(':checked');
            if (bischecked) {
                $.ajax({
                    url: 'http://api.hongyanche.com/forum_admin/label_release',
                    type: 'POST',
    xhrFields:{
    withCredentials:true
    },
                    data: {
                        label_id: ID,
                        release: 1
                    },
                    success: function () {
                        initPage('reload');
                    }
                })
            } else {
                $.ajax({
                    url: 'http://api.hongyanche.com/forum_admin/label_release',
                    type: 'POST',
    xhrFields:{
    withCredentials:true
    },
                    data: {
                        label_id: ID,
                        release: 0
                    },
                    success: function () {
                        initPage('reload');
                    }
                })
            }
        });

        // admin发帖  /setadminlabel
        $('#forum-name').on('change', '.admin', function () {
            var ID = $(this).attr('data-id');
            var bischecked = $($(this)).is(':checked');
            if (bischecked) {
                $.ajax({
                    url: 'http://api.hongyanche.com/forum_admin/setadminlabel',
                    type: 'POST',
    xhrFields:{
    withCredentials:true
    },
                    data: {
                        label_id: ID,
                        release: 1
                    },
                    success: function () {
                        initPage('reload');
                    }
                })
            } else {
                $.ajax({
                    url: 'http://api.hongyanche.com/forum_admin/setadminlabel',
                    type: 'POST',
    xhrFields:{
    withCredentials:true
    },
                    data: {
                        label_id: ID,
                        release: 0
                    },
                    success: function () {
                        initPage('reload');
                    }
                })
            }
        });


    })(window, undefined)
</sss>